<?php

class Team {
	private $teamnumber;
	private $name;
	private $informalname;
	private $fullinformalname;
	private $city;
	private $state;
	private $country;
	private $rookieyear;
	private $motto;
	private $robotname;
	private $website;
	
	// CONTRACT: __construct : number -> Team
	// PURPOSE
	// __construct is the auto-called function that builds a new Team. It takes a $teamnumber and returns the Team object for the corresponding team in the database.
	
	function __construct($teamnumber) {
		//ensure valid input
		if (!is_numeric($teamnumber)) {
			throw_error("Non-numeric teamnumber for Team Object instantantiation. Given " . $teamnumber);
		}
		//setup and run query
		$teaminfo_query_filter = "SELECT * FROM teams WHERE teamnumber='$teamnumber' LIMIT 1";
		$teaminfo_query = mysql_query($teaminfo_query_filter);
		if (mysql_num_rows($teaminfo_query) < 1) {
			throw_error("No team found for teamnumber for Team Object instantiation. Given " . $teamnumber);
		}
		//copy into object
		$teaminfo = mysql_fetch_array($teaminfo_query);
		$this->teamnumber 	= $teamnumber;
		$this->name 		= $teaminfo["name"];
		$this->informalname = $teaminfo["informalname"];
		$this->city 		= $teaminfo["city"];
		$this->state 		= $teaminfo["state"];
		$this->country 		= $teaminfo["country"];
		$this->rookieyear 	= $teaminfo["rookieyear"];
		$this->motto 		= $teaminfo["motto"];
		$this->robotname 	= $teaminfo["robotname"];
		$this->website 		= $teaminfo["website"];
		
		if ($this->informalname != "") {
			$this->fullinformalname = $this->informalname . " - Team " . $this->teamnumber;
		} else {
			$this->fullinformalname = "Team Number " . $this->teamnumber;
		}			
	}
	
	function getFullInformalName() {
		return $this->fullinformalname;
	}
	function getInformalName() {
		return $this->informalname;
	}
	function getTeamNumber() {
		return $this->teamnumber;
	}
	function getName() {
		return $this->name;
	}
	function getCity() {
		return $this->city;
	}
	function getState() {
		return $this->state;
	}
	function getCountry() {
		return $this->country;
	}
	function getLocation() {
	    $locationArray = array();
	    if ($this->city) $locationArray[] = $this->city;
	    if ($this->state) $locationArray[] = $this->state;
        if ($this->country) $locationArray[] = $this->country;
        return join(", ", $locationArray);
	}
	function getRookieYear() {
		return $this->rookieyear;
	}
	function getWebsite() {
	    return $this->website;
	}
	function getEvents($year = Null) {
		if ($year == Null) $year = date('Y');
		$events_query_filter = "SELECT attendance.event FROM attendance, events WHERE team=" . $this->getTeamNumber() . " AND events.eventid = attendance.event AND events.year = " . $year . " ORDER BY events.week ASC;";
		$events_query = mysql_query($events_query_filter);
		
		//construct list
		$list_of_events = array();
		while ($event = mysql_fetch_array($events_query)) {
			if ($event["event"] > 0) {
				$event_object = new Event($event["event"]);
			$list_of_events[] = $event_object;
			}
		}
		
		//send the list back.
		return $list_of_events;
	}
	
	function getOfficialEvents($year = Null) {
		if ($year == Null) $year = date('Y');
		$events_query_filter = "SELECT attendance.event FROM attendance, events WHERE team=" . $this->getTeamNumber() . " AND events.eventid = attendance.event AND events.year = " . $year . " AND official = 1 ORDER BY events.week ASC;";
		$events_query = mysql_query($events_query_filter);
		
		//construct list
		$list_of_events = array();
		while ($event = mysql_fetch_array($events_query)) {
			if ($event["event"] > 0) {
				$event_object = new Event($event["event"]);
			$list_of_events[] = $event_object;
			}
		}
		
		//send the list back.
		return $list_of_events;
	}
	
	// CONTRACT: getPhoto : number -> string
	// PURPOSE
	// getPhoto takes a year, and returns, if available, a string that is the URL to a photo of the team's robot from that year. If no photo is available it returns null.
	function getPhoto($year) {
		if (file_exists("../tbatv/pictures/".$year."/".$this->getTeamNumber().".jpg")) {
			return "/tbatv/pictures/".$year."/".$this->getTeamNumber().".jpg";
		} else {
			return Null;
		}
	}
	
	// CONTRACT: getPhotoThumb : number -> string
	// PURPOSE
	// getPhoto takes a year, and returns, if available, a string that is the URL to a thumbnail of a photo of the team's robot from that year. If no thumbnail photo is available it returns null.
	function getPhotoThumb($year) {
		if (file_exists("../tbatv/pictures/".$year."/thumbs/".$this->getTeamNumber().".jpg")) {
			return "/tbatv/pictures/".$year."/thumbs/".$this->getTeamNumber().".jpg";
		} else {
			return Null;
		}
	}
	// CONTRACT: getWLT : number, number -> array
	// PURPOSE
	// getWLT($span, $id) returns the win-lose-tie results of a team during an event's qualifications, an event's eliminations, an entire event, an entire year, or ever.
	function getWLT($span, $id) {
		//$span can be "quals", "elims", "event", "year", "alltime"
		
		//$span is "quals", $id is an eventid
		//$span is "elims", $id is an eventid
		//$span is "event", $id is an eventid
		//$span is "year", $id is a year
		//$span is "alltime", $id doesn't matter
		
		switch($span) {
			case "quals":
				$win_query_filter = "SELECT * FROM matches WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore > bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore > redscore)) AND complevel = 10 AND event = $id";
				$lose_query_filter = "SELECT * FROM matches WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore < bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()})) AND bluescore < redscore) AND complevel = 10 AND event = $id";
				$tie_query_filter = "SELECT * FROM matches WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()} OR blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore = redscore)) AND bluescore > -1 AND redscore > -1 AND complevel = 10 AND event = $id";
				break;
				
			case "elims":
				$win_query_filter = "SELECT * FROM matches WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore > bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore > redscore)) AND complevel > 10 AND event = $id";
				$lose_query_filter = "SELECT * FROM matches WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore < bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()})) AND bluescore < redscore) AND complevel > 10 AND event = $id";
				$tie_query_filter = "SELECT * FROM matches WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()} OR blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore = redscore)) AND bluescore > -1 AND redscore > -1 AND complevel > 10 AND event = $id";
				break;
			
			case "event":
				$win_query_filter = "SELECT * FROM matches WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore > bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore > redscore)) AND event = $id";
				$lose_query_filter = "SELECT * FROM matches WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore < bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()})) AND bluescore < redscore) AND event = $id";
				$tie_query_filter = "SELECT * FROM matches WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()} OR blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore = redscore)) AND bluescore > -1 AND redscore > -1 AND event = $id";
				break;
				
			case "year":
				$win_query_filter = "SELECT * FROM matches, events WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore > bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore > redscore)) AND matches.event = events.eventid AND events.year = $id AND events.official = 1";
				$lose_query_filter = "SELECT * FROM matches, events WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore < bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()})) AND bluescore < redscore) AND matches.event = events.eventid AND events.year = $id AND events.official = 1";
				$tie_query_filter = "SELECT * FROM matches, events WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()} OR blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore = redscore)) AND bluescore > -1 AND redscore > -1 AND matches.event = events.eventid AND events.year = $id AND events.official = 1";
				break;
				
			case "alltime":
				$win_query_filter = "SELECT * FROM matches, events WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore > bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore > redscore)) AND matches.event = events.eventid AND events.official = 1";
				$lose_query_filter = "SELECT * FROM matches, events WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()}) AND redscore < bluescore) OR ((blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()})) AND bluescore < redscore) AND matches.event = events.eventid AND events.official = 1";
				$tie_query_filter = "SELECT * FROM matches, events WHERE (((red1 = {$this->getTeamNumber()} OR red2 = {$this->getTeamNumber()} OR red3 = {$this->getTeamNumber()} OR blue1 = {$this->getTeamNumber()} OR blue2 = {$this->getTeamNumber()} OR blue3 = {$this->getTeamNumber()}) AND bluescore = redscore)) AND bluescore > -1 AND redscore > -1 AND matches.event = events.eventid AND events.official = 1";
				break;
		}
			
		$record_win_query = mysql_query($win_query_filter);
		$record["win"] = mysql_num_rows($record_win_query);
		
		$record_lose_query = mysql_query($lose_query_filter);
		$record["lose"] = mysql_num_rows($record_lose_query);
		
		$record_tie_query = mysql_query($tie_query_filter);
		$record["tie"] = mysql_num_rows($record_tie_query);
		
		return $record;
	}
	
	// CONTRACT: getEventWins: number, boolean -> number
	// PURPOSE
	// getEventWins() returns how many events this team has won in a certain year, defaulting to this year. It optionally takes a flag about whether or not the events must be official, which defaults to true.
	function getEventWins($year = Null, $official = TRUE){
		$events_won = 0;
		if ($year == Null) $year = date('Y');
		
		if ($official == TRUE) {
			$list_of_events = $this->getOfficialEvents($year);
		} else {
			$list_of_events = $this->getEvents($year);
		}
		
		foreach ($list_of_events as $event) {
			$record = $this->getWLT("elims",$event->getEventID());
			if( $record["win"] > 5 ){
				$events_won += 1;
			}
		}
		return $events_won;
	}

	
	// CONTRACT: linkto() : number, string-> string
	// PURPOSE
	// linkto() takes (optionally) a number and (optionall) a string, and returns an HTML formatted anchor tag linking to the team's specific page for a specific year.
	function linkto($year = Null, $string = Null) {
		global $BASE_PATH;
		global $MOD_REWRITE_ON;
		if ($string == Null) $string = "Team " . $this->getTeamNumber();
		if ($year == Null) $year = date('Y');
		if ($MOD_REWRITE_ON) {
		    return "<a href=\"" . $BASE_PATH . "team/" . $this->getTeamNumber() . "/" . $year . "\" title=\"" . $string . "\">" . $string . "</a>";
		} else {
		    return "<a href=\"" . $BASE_PATH . "team.php?team=" . $this->getTeamNumber() . "&year=" . $year . "\" title=\"" . $string . "\">" . $string . "</a>";
		}
	}
	 
}

?>